using System.Diagnostics;

namespace 多线程
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string test = "";
        List<Colors> list = new List<Colors>();
        private void button1_Click(object sender, EventArgs e)
        {
            //ImageManager.Cut(filePath, 100, 100, "E:/cs/多线程/多线程/bin/Debug/net6.0-windows/图", "png");
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Bitmap[] bitmap = null;
            //var task = Task.Run(() =>
            //{
                 bitmap = ImageManager.Bimpa(filePath, 100, 100, "E:/cs/多线程/多线程/bin/Debug/net6.0-windows/图", "png");
            //});
           // task.Wait();
            for (int i = 0; i < bitmap.Count(); i++)
            {
                var res = Task.Run(() =>
                {
                    GetImg1(bitmap[i]);
                });
                res.Wait();
            }
            label2.Text = list.Count().ToString();
            list.Clear();
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            label3.Text = $"执行总共花费{ts2.TotalMilliseconds}ms.";

        }
        int aa1 = 0;
        public void GetImg1(Bitmap bitmap)
        {
            int ImgWidth = bitmap.Width;//图片宽度
            int ImgHeight = bitmap.Height;//图片高度
                                          //获取坐标下的颜色值（a,r,g,b)
            Color pixelColor = bitmap.GetPixel(ImgWidth - 1, ImgHeight - 1);//0-999共1000像素
            for (int i = 0; i < ImgWidth; i++)
            {
                for (int i1 = 0; i1 < ImgHeight; i1++)
                {
                    //Bimg.SetPixel(i, i1, Color.Red);
                    Color cl = bitmap.GetPixel(x: i, i1);
                    //ys += "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    Colors s = new Colors();
                    //s.Color = "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    s.Color =cl.R+","+cl.G+","+cl.B;
                    s.Row = "第" + i + "行，" + "第" + i1 + "列";
                    list.Add(s);
                    aa1++;
                }
            }

        }
        public  void CallToChildThread()
        {
        }
        string filePath = "";
        string filePath2 = "";
        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            list.Clear();
            Bitmap Bimg = new Bitmap(filePath);
            GetImg1(Bimg);
            label5.Text = list.Count().ToString();
            var sa = list.GroupBy(c => c.Color).Select(c => c.First()).ToList();
            label6.Text = sa.Count().ToString();
            dataGridView1.DataSource = sa.OrderBy(c=>c.Color).ToList();
           
            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            label4.Text = $"执行总共花费{ts2.TotalMilliseconds}ms.";
        }
        public void GetImg1d0(Bitmap bitmap)
        {
            int ImgWidth = bitmap.Width;//图片宽度
            int ImgHeight = bitmap.Height;//图片高度
                                          //获取坐标下的颜色值（a,r,g,b)
            Color pixelColor = bitmap.GetPixel(ImgWidth - 1, ImgHeight - 1);//0-999共1000像素
            for (int i = 0; i < ImgWidth; i++)
            {
                for (int i1 = 0; i1 < ImgHeight; i1++)
                {
                    //Bimg.SetPixel(i, i1, Color.Red);
                    Color cl = bitmap.GetPixel(x: i, i1);
                    //ys += "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    Colors s = new Colors();
                    s.Color = "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    list.Add(s);

                }
            }

        }
        public void GetImg1d1(Bitmap bitmap)
        {
            int ImgWidth = bitmap.Width;//图片宽度
            int ImgHeight = bitmap.Height;//图片高度
                                          //获取坐标下的颜色值（a,r,g,b)
            Color pixelColor = bitmap.GetPixel(ImgWidth - 1, ImgHeight - 1);//0-999共1000像素
            for (int i = 0; i < ImgWidth; i++)
            {
                for (int i1 = 0; i1 < ImgHeight; i1++)
                {
                    //Bimg.SetPixel(i, i1, Color.Red);
                    Color cl = bitmap.GetPixel(x: i, i1);
                    //ys += "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    Colors s = new Colors();
                    s.Color = "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    list.Add(s);

                }
            }

        }


        private void Form1_Load(object sender, EventArgs e)
        {
             
        }

        private void textBox2111_Click(object sender, EventArgs e)
        {
            //创建文件弹出选择窗口（包括文件名）对象
            OpenFileDialog ofd = new OpenFileDialog();
            //判断选择的路径
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox2111.Text = ofd.FileName.ToString();
                pictureBox2.Image = Image.FromFile(ofd.FileName.ToString());

            }
            filePath2 = textBox2111.Text;
        }
        double bfb = 0.00;
        List<Colors> listxiu =new List<Colors>();
        private void button3_Click(object sender, EventArgs e)
        {

            Stopwatch sw = new Stopwatch();
            sw.Start();
            listxiu.Clear();
            Bitmap Bimg = new Bitmap(filePath2);
            bfb = 0;
             bfb=GetImgmj(Bimg);
            //list=list.GroupBy(c => c.Color).Select(c => c.First()).ToList();
            
            // List<Colors> listxiu = data;
            //Task.Run(() =>
            //{
            //    for (int i = 0; i < list.Count()/2; i++)
            //    {
            //        for (int j = 0; j < listxiu.Count()/2; j++)
            //        {
            //            if (list[i].Color == listxiu[j].Color)
            //            {
            //                bfb++;
            //            }
            //        }

            //    }
            //}); 
            //Task.Run(() =>
            //{
            //    for (int i1 = list.Count()-1; i1 > (list.Count()/2); i1--)
            //    {
            //        for (int j1 = listxiu.Count()-1; j1 > (listxiu.Count()/2); j1--)
            //        {
            //            if (list[i1].Color == listxiu[j1].Color)
            //            {
            //                bfb++;
            //            }
            //        }

            //    }
            //}).Wait();

            sw.Stop();
            TimeSpan ts2 = sw.Elapsed;
            label9.Text=(100-(bfb/(Bimg.Width*Bimg.Height)*100)).ToString();
            
            label8.Text = $"执行总共花费{ts2.TotalMilliseconds}ms.";
            MessageBox.Show(bfb.ToString());
            bfb = 0;
        }


        public double GetImgmj(Bitmap bitmap)
        {
            int ImgWidth = bitmap.Width;//图片宽度
            int ImgHeight = bitmap.Height;//图片高度
                                          //获取坐标下的颜色值（a,r,g,b)
            Color pixelColor = bitmap.GetPixel(ImgWidth - 1, ImgHeight - 1);//0-999共1000像素
            for (int i = 0; i < ImgWidth; i++)
            {
                for (int i1 = 0; i1 < ImgHeight; i1++)
                {
                    //Bimg.SetPixel(i, i1, Color.Red);
                    Color cl = bitmap.GetPixel(x: i, i1);
                    //ys += "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    Colors s = new Colors();
                    //s.Color = "#" + cl.R.ToString("X").PadLeft(2, '0') + cl.G.ToString("X").PadLeft(2, '0') + cl.B.ToString("X").PadLeft(2, '0');//输出16进制颜色
                    //s.Color = cl.R+"," + cl.G + "," + cl.B;
                    //listxiu.Add(s);
                    //if (cl.R<=242&&cl.R>=71&&cl.G<=255&&cl.G>=89&&cl.B>=255&&cl.B<=93)
                    //{
                    //    bfb++;
                    //}
                //if (cl.R <= 205 && cl.R >= 54 && cl.G <= 208 && cl.G >= 39 && cl.B <= 206 && cl.B >= 32)
                //{
                //    bfb++;
                //}
                    if (cl.R <= 246 && cl.R >= 63 && cl.G <= 211 && cl.G >= 36 && cl.B <= 175 && cl.B >= 17)
                    {
                        bfb++;
                    }

                }
            }
            return bfb;
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            //创建文件弹出选择窗口（包括文件名）对象
            OpenFileDialog ofd = new OpenFileDialog();
            //判断选择的路径
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = ofd.FileName.ToString();
                pictureBox1.Image = Image.FromFile(ofd.FileName.ToString());

            }
            filePath = textBox1.Text;
        }
    }
}   